package org.example.controller;

import org.example.annotation.CheckPermission;
import org.example.pojo.domain.Reservation;
import org.example.result.Result;
import org.example.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/reservations")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    // 获取某用户所有预约记录
    @CheckPermission({"visitor","admin"})
    @GetMapping("/user/{userId}")
    public Result listReservations(@PathVariable Integer userId) {
        List<Reservation> reservations = reservationService.listReservationsByUser(userId);
        return Result.success("用户("+userId+")所有预约记录",reservations);
    }

    // 创建新预约
    @CheckPermission({"visitor"})
    @PostMapping("/make")
    public Result makeReservation(@RequestBody Reservation reservation) {
        reservationService.makeReservation(reservation);
        return Result.success("预约成功！");
    }

    // 取消预约
    @CheckPermission({"visitor"})
    @DeleteMapping("/cancel/{reservationId}")
    public Result cancelReservation(@PathVariable Integer reservationId) {
        reservationService.cancelReservation(reservationId);
        return Result.success("预约已取消！");
    }

    // 获取预约详情
    @CheckPermission({"visitor","admin"})
    @GetMapping("/{reservationId}")
    public Result getReservationDetails(@PathVariable Integer reservationId) {
        Reservation reservation = reservationService.getReservationDetails(reservationId);
        return Result.success("预约id("+reservationId+")的详情",reservation);
    }
}
